package Spider;


import DBC.DatabaseConnection;
import Factory.DAOFactory;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @program: 爬虫
 * @description: 测试课程表保存
 * @author: 王代涛
 * @create: 2021-05-12 22:47
 **/
@SuppressWarnings("JavaDoc")
public class getWeek {
    public static boolean forCourse(String username, String password, Connection conn) throws NullPointerException{
        String indexUrl = "http://class.sise.com.cn:7001/sise/";
        String loginUrl = "http://class.sise.com.cn:7001/sise/login_check_login.jsp";
        String targetUrl = "http://class.sise.com.cn:7001/sise/module/student_schedular/student_schedular.jsp";
        CloseableHttpResponse response;
        int successCode = 200;
        //创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建httpget对象,设置url访问地址
        HttpGet httpGet = new HttpGet(indexUrl);
        try {
            //使用httpclient发送请求,获取response
            response = httpClient.execute(httpGet);
            //解析响应
            if (response.getStatusLine().getStatusCode() == successCode){
                String content = EntityUtils.toString(response.getEntity(),"utf-8");
                //解析indexUrl
                Document doc = Jsoup.parse(content);
                Element element = doc.select("input[name]").first();
                //parmName,parmValue,parmRandom为登陆的post参数
                String parmName = element.attr("name");
                String parmValue = element.attr("value");

                if (parmName!=null&&parmValue!=null){
                    HttpPost httpPost = new HttpPost(loginUrl);

                    //封装登陆参数
                    ArrayList<NameValuePair> params = new ArrayList<>();
                    params.add(new BasicNameValuePair(parmName,parmValue));
                    params.add(new BasicNameValuePair("username",username));
                    params.add(new BasicNameValuePair("password",password));
                    //创建表单entity对象
                    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "utf-8");
                    httpPost.setEntity(formEntity);

                    response = httpClient.execute(httpPost);
                    if (response.getStatusLine().getStatusCode() == successCode){
                        httpGet = new HttpGet(targetUrl);
                        response = httpClient.execute(httpGet);
                        if (response.getStatusLine().getStatusCode() == successCode){
                            content = EntityUtils.toString(response.getEntity());
                            //解析targetUrl
                            Document name = Jsoup.parse(content);
                            //获取当前学周
                            Element schoolTime = name.select(".style16").last();
                            Scanner scanner3 = new Scanner(schoolTime.text());
                            scanner3.useDelimiter(":\\s");
                            scanner3.next();
                            String ThisWeek = scanner3.next();
                        }
                    }
                    return true;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}
